home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / SFILTER.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  10KB  |  383 lines

  1. /* Copyright (C) 1991, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* sfilter.c */
  20. /* Stream functions for filters */
  21. #include "stdio_.h"    /* includes std.h */
  22. #include "memory_.h"
  23. #include "scanchar.h"
  24. #include "stream.h"
  25. #include "gscrypt1.h"
  26.  
  27. /* ------ Generic functions ------ */
  28.  
  29. /* Implement flushing for all encoding filters. */
  30. int
  31. s_filter_write_flush(register stream *s)
  32. {    (*s->procs.write_buf)(s);
  33.     return sflush(s->strm);
  34. }
  35.  
  36. /* Close a filter.  If this is an encoding filter, flush it first. */
  37. int
  38. s_filter_close(register stream *s)
  39. {    if ( s_is_writing(s) )
  40.        {    int code = sflush(s);
  41.         if ( code == ERRC ) return code;
  42.        }
  43.     return s_std_close(s);
  44. }    
  45.  
  46. /* ------ ASCIIHexEncode ------ */
  47.  
  48. /* Flush the buffer */
  49. private int
  50. s_AXE_write_buf(register stream *s)
  51. {    register stream *strm = s->strm;
  52.     register byte *p = s->cbuf;
  53.     register int count = s->cptr + 1 - p;
  54.     register const char _ds *hex_digits = "0123456789abcdef";
  55.     while ( --count >= 0 )
  56.       { char c1 = hex_digits[*p >> 4];
  57.         char c2 = hex_digits[*p & 0xf];
  58.         sputc(strm, c1);
  59.         sputc(strm, c2);
  60.         if ( !(count & 15) )
  61.           sputc(strm, '\n');
  62.         p++;
  63.       }
  64.     s->cptr = s->cbuf - 1;
  65.     return 0;
  66. }
  67.  
  68. /* Close the stream */
  69. private int
  70. s_AXE_close(register stream *s)
  71. {    (*s->procs.write_buf)(s);
  72.     sputc(s->strm, '>');
  73.     return s_std_close(s);
  74. }
  75.  
  76. /* Stream procedures */
  77. const stream_procs s_AXE_procs =
  78.    {    s_std_noavailable, NULL, s_filter_write_flush, s_AXE_close,
  79.     NULL, s_AXE_write_buf
  80.    };
  81.  
  82. /* ------ ASCIIHexDecode ------ */
  83.  
  84. /* Initialize the stream */
  85. void
  86. s_AXD_init(register stream *s)
  87. {    s->odd = -1;
  88. }
  89.  
  90. /* Refill the buffer */
  91. private int
  92. s_AXD_read_buf(register stream *s)
  93. {    stream *strm = s->strm;
  94.     uint count;
  95.     int code = sreadhex(strm, s->cbuf, s->bsize, &count, &s->odd, 0);
  96.     s->cptr = s->cbuf - 1;
  97.     s->endptr = s->cptr + count;
  98.     if ( code != ERRC )
  99.     {    if ( code )
  100.             s->end_status = strm->end_status;
  101.         return 0;
  102.     }
  103.     /* Check for EOD */
  104.     if ( sgetc(strm) == '>' )    /* EOD */
  105.         s->end_status = EOFC;
  106.     else            /* syntax error */
  107.        {    s->end_status = ERRC;
  108.         sputback(strm);
  109.        }
  110.     return 0;
  111. }
  112.  
  113. /* Stream procedures */
  114. const stream_procs s_AXD_procs =
  115.    {    s_std_noavailable, s_std_noseek, s_std_read_flush, s_filter_close,
  116.     s_AXD_read_buf, NULL
  117.    };
  118.  
  119. /* ------ eexecDecode ------ */
  120.  
  121. /* Initialize a stream for reading and decrypting another stream. */
  122. /* Decrypting streams are not positionable. */
  123. void
  124. s_exD_init(register stream *s, ushort /*crypt_state*/ state)
  125. {    s->cstate = state;
  126.     s->odd = -1;
  127.     s->binary = -1;            /* unknown */
  128. }
  129.  
  130. /* Refill the buffer of a decrypting stream. */
  131. private int
  132. s_exD_read_buf(register stream *s)
  133. {    byte *buf = s->cbuf;
  134.     uint nread;
  135.     int skip = (s->binary < 0 ? 4 : 0);
  136.     s->cptr = s->endptr = buf - 1;
  137. top:    nread = sgets(s->strm, buf, s->bsize);
  138.     if ( nread == 0 )        /* end of stream */
  139.        {    s->end_status = EOFC;
  140.         return 0;
  141.        }
  142.     if ( s->binary < 0 )
  143.        {    /* This is the very first time we're filling the buffer. */
  144.         /* Determine whether this is ASCII or hex encoding. */
  145.         register byte _ds *decoder = scan_char_decoder;
  146.         if ( nread < 4 ) return EOFC;
  147.         if ( decoder[buf[0]] == ctype_space ||
  148.              (decoder[buf[0]] | decoder[buf[1]] | decoder[buf[2]] |
  149.               decoder[buf[3]]) <= 0xf )
  150.            {    /* Would be invalid if binary, hence must be hex. */
  151.             s->binary = 0;
  152.            }
  153.         else    s->binary = 1;
  154.             
  155.        }
  156.     if ( !s->binary )
  157.        {    /* Convert the buffer from binary to hex in place. */
  158.         stream sst;
  159.         sread_string(&sst, buf, nread);
  160.         sreadhex(&sst, buf, nread, &nread, &s->odd, 0);
  161.         if ( nread == 0 ) goto top;    /* try again */
  162.        }
  163.     /* Now decrypt the buffer. */
  164.     gs_type1_decrypt(buf, buf, nread, (crypt_state *)&s->cstate);
  165.     if ( skip )
  166.        {    /* Very first buffer-load, strip off leading bytes. */
  167.         if ( nread < skip ) return EOFC;
  168.         s->cptr += skip;
  169.         nread -= skip;
  170.        }
  171.     s->endptr = s->cptr + nread;
  172.     return 0;
  173. }
  174.  
  175. /* Estimate the number of remaining bytes in a decrypting stream. */
  176. private int
  177. s_exD_available(stream *s, long *pl)
  178. {    if ( savailable(s->strm, pl) < 0 ) return ERRC;
  179.     if ( *pl >= 0 ) *pl /= 2;
  180.     return 0;
  181. }
  182.  
  183. /* Stream procedures */
  184. const stream_procs s_exD_procs =
  185.    {    s_exD_available, s_std_noseek, s_std_read_flush, s_filter_close,
  186.     s_exD_read_buf, NULL
  187.    };
  188.  
  189. /* ------ NullEncode ------ */
  190.  
  191. /* Flush the buffer */
  192. private int
  193. s_NullE_write_buf(register stream *s)
  194. {    uint count = s->cptr - s->cbuf + 1;
  195.     sputs(s->strm, s->cbuf, count);
  196.     s->cptr = s->cbuf - 1;
  197.     s->position += count;
  198.     return 0;
  199. }
  200.  
  201. /* Stream procedures */
  202. const stream_procs s_NullE_procs =
  203.    {    s_std_noavailable, s_std_noseek, s_filter_write_flush, s_filter_close,
  204.     NULL, s_NullE_write_buf
  205.    };
  206.  
  207. /* ------ PFBDecode ------ */
  208.  
  209. /* Initialize the stream */
  210. void
  211. s_PFBD_init(register stream *s, int binary_to_hex)
  212. {    s->record_type = -1;
  213.     s->binary_to_hex = binary_to_hex;
  214. }
  215.  
  216. /* Refill the buffer */
  217. private int
  218. s_PFBD_read_buf(register stream *s)
  219. {    stream *strm = s->strm;
  220.     register byte *ptr = s->cbuf;
  221.     uint count;
  222.     int c;
  223.     s->cptr = s->endptr = s->cbuf - 1;
  224. top:    count = s->bsize;
  225.     switch ( s->record_type )
  226.        {
  227.     case -1:            /* new record */
  228.         c = sgetc(strm);
  229.         if ( c != 0x80 ) goto err;
  230.         c = sgetc(strm);
  231.         switch ( c )
  232.            {
  233.         case 1: case 2:
  234.             s->record_type = c;
  235.             break;
  236.         case 3:
  237.         case EOFC:
  238.             s->end_status = EOFC;
  239.             return 0;
  240.         default:
  241.             goto err;
  242.            }
  243.         s->record_left = sgetc(strm);
  244.         s->record_left += (ulong)sgetc(strm) << 8;
  245.         s->record_left += (ulong)sgetc(strm) << 16;
  246.         s->record_left += (ulong)sgetc(strm) << 24;
  247.         goto top;
  248.     case 1:                /* text data */
  249.         /* Translate \r to \n. */
  250.         if ( count > s->record_left ) count = s->record_left;
  251.        {    uint n;
  252.         for ( n = count; n != 0 && (c = sgetc(strm)) != EOFC; n-- )
  253.            {    *ptr++ = (c == '\r' ? '\n' : c);
  254.            }
  255.        }    break;
  256.     case 2:                /* binary data */
  257.         if ( s->binary_to_hex )
  258.         {    /* Translate binary to hex. */
  259.             uint n;
  260.             byte *inp;
  261.             register const char _ds *hex_digits = "0123456789abcdef";
  262.             count >>= 1;        /* 2 chars per input byte */
  263.             if ( count > s->record_left ) count = s->record_left;
  264.             inp = ptr + count;
  265.             count = sgets(strm, inp, count);
  266.             for ( n = count; n != 0; n-- )
  267.             {    c = *inp++;
  268.                 ptr[0] = hex_digits[c >> 4];
  269.                 ptr[1] = hex_digits[c & 0xf];
  270.                 ptr += 2;
  271.             }
  272.         }
  273.         else
  274.         {    /* Just read binary data. */
  275.             if ( count > s->record_left ) count = s->record_left;
  276.             count = sgets(strm, ptr, count);
  277.             ptr += count;
  278.         }
  279.         break;
  280.        }
  281.     if ( count == 0 )
  282.        {    s->record_type = -1;
  283.         goto top;
  284.        }
  285.     s->record_left -= count;
  286.     s->endptr = ptr - 1;
  287.     return 0;
  288. err:    s->end_status = ERRC;
  289.     s->endptr = ptr - 1;
  290.     return 0;
  291. }
  292.  
  293. /* Stream procedures */
  294. const stream_procs s_PFBD_procs =
  295.    {    s_std_noavailable, s_std_noseek, s_std_read_flush, s_filter_close,
  296.     s_PFBD_read_buf, NULL
  297.    };
  298.  
  299. /* ------ SubFileDecode ------ */
  300.  
  301. /* Initialize the stream */
  302. void
  303. s_SFD_init(stream *s, long count, const byte *str, uint strlen)
  304. {    s->state.sf.count = count;
  305.     s->state.sf.eod_string = str;
  306.     s->state.sf.string_size = strlen;
  307.     s->state.sf.match = 0;
  308. }
  309.  
  310. /* Refill the buffer */
  311. private int
  312. s_SFD_read_buf(register stream *s)
  313. {    stream *strm = s->strm;
  314.     if ( s->state.sf.string_size == 0 )
  315.        {    /* Just read, with no EOD pattern. */
  316.         uint count;
  317.         if ( s->state.sf.count == 0 )
  318.            {    /* Read with no EOD limit. */
  319.             count = sgets(strm, s->cbuf, s->bsize);
  320.             s->end_status = strm->end_status;
  321.            }
  322.         else
  323.            {    /* Count bytes till EOD. */
  324.             uint len = min(s->bsize, s->state.sf.count);
  325.             count = sgets(strm, s->cbuf, len);
  326.             s->end_status = ((s->state.sf.count -= count) == 0 ?
  327.                      EOFC : strm->end_status);
  328.            }
  329.         s->cptr = s->cbuf - 1;
  330.         s->endptr = s->cptr + count;
  331.        }
  332.     else
  333.        {    /* Read looking for an EOD pattern. */
  334.         uint keep = s->endptr + 1 - s->cbuf;
  335.         byte *p = s->cbuf - 1;
  336.         byte *limit = p + s->bsize;
  337.         uint match = s->state.sf.match;
  338.         if ( keep > match ) keep = match;
  339.         memcpy(s->cbuf, s->endptr + 1 - keep, keep);
  340.         s->cptr = p + keep;
  341.         while ( p < limit )
  342.            {    int c = sgetc(strm);
  343.             if ( c < 0 )        /* premature EOF */
  344.                {    s->end_status = c;
  345.                 s->endptr = p;
  346.                 return 0;
  347.                }
  348.             *++p = c;
  349.             if ( c == s->state.sf.eod_string[match] )
  350.                {    if ( ++match == s->state.sf.string_size )
  351.                    {    if ( s->state.sf.count <= 1 )
  352.                        {    s->endptr =
  353.                           (s->state.sf.count == 0 ?
  354.                            p - s->state.sf.string_size :
  355.                            p);
  356.                         s->end_status = EOFC;
  357.                         return 0;
  358.                        }
  359.                     s->state.sf.count--;
  360.                     match = 0;
  361.                    }
  362.                }
  363.             /* No match here, back up to find the longest one. */
  364.             /* This may be quadratic in string_size, but */
  365.             /* we don't expect this to be a real problem. */
  366.             for ( ; match != 0; match-- )
  367.               if ( !memcmp(s->state.sf.eod_string, p - match + 1,
  368.                        match)
  369.                  )
  370.                 break;
  371.            }
  372.         s->endptr = p;
  373.         s->state.sf.match = match;
  374.        }
  375.     return 0;
  376. }
  377.  
  378. /* Stream procedures */
  379. const stream_procs s_SFD_procs =
  380.    {    s_std_noavailable, s_std_noseek, s_std_read_flush, s_filter_close,
  381.     s_SFD_read_buf, NULL
  382.    };
  383.